export default {
    methods: {
      /**
       * 取消订单
       *
       * @param {string} orderId - 订单ID
       * @param {number} index - 订单在列表中的索引
       */
      cancelOrder(orderId, index, type) {
        this.$api.sendRequest({
          url: "/canteen/api/order/close", 
          data: {
            order_id: orderId, 
          },
          success: res => {
            if (res.code == 0) {
              this.$util.showToast({ title: '订单取消成功' });
              if(type == 'list') {
                this.page = 1;
                this.fetchOrders(); // 刷新订单列表数据
              }else {
                this.getOrderDetail();
              }
            } else {
              this.$util.showToast({ title: res.message });
            }
          }
        });
      },
      /**
       * 确认收货
       *
       * @param {number} orderId - 订单ID
       */
      confirmReceipt(orderId,type) {
        this.$api.sendRequest({
          url: "/api/order/takedelivery", 
          data: {
            order_id: orderId, 
          },
          success: res => {
            if (res.code == 0) {
              this.$util.showToast({ title: '确认收货成功' });
              if(type == 'list') {
                this.page = 1; // 重置页码为第一页
                this.fetchOrders(); // 刷新订单列表数据
              }else {
                this.getOrderDetail();
              }
  
            } else {
              this.$util.showToast({ title: res.message });
            }
          },
          fail: err => {
            this.$util.showToast({ title: '确认收货失败，请重试' });
          }
        });
      },
      /**
       * 支付订单
       */
      payOrder(out_trade_no,type) {
        this.$api.sendRequest({
          url: "/api/pay/pay",
          data: {
            out_trade_no: out_trade_no,
            pay_type: "wechatpay"
          },
          success: res => {
            if (res.code == 0) {
                var payData = res.data.data;
                this.pay(payData,type)
            } else {
                this.$util.showToast({ title: res.message });
            }
          }
        })
      },
      goMiniProgram(data,type) {
        var that = this;
        uni.navigateToMiniProgram({
          appId: data.appid,
          path: data.path,
          extraData: {
          },
          success(res) {
            if(type == 'list') {
              that.page = 1; // 重置页码为第一页
              that.fetchOrders(); // 刷新订单列表数据
            }else {
              that.getOrderDetail();
            }
          }
        })
      },
      /**
       * 发起支付请求
       *
       * @param {Object} payData 支付数据对象
       * @param {number} payData.timeStamp 时间戳
       * @param {string} payData.nonceStr 随机字符串
       * @param {string} payData.package 订单详情扩展字符串
       * @param {string} payData.signType 签名方式
       * @param {string} payData.paySign 签名
       */
      pay(payData,type) {
        uni.requestPayment({
          provider: 'wxpay',
          timeStamp: payData.timeStamp,
          nonceStr: payData.nonceStr,
          package: payData.package,
          signType: payData.signType,
          paySign: payData.paySign,
          success: res => {
            this.$util.showToast({ title: "支付成功" });
            if(type == 'list') {
              this.page = 1; // 重置页码为第一页
              this.fetchOrders(); // 刷新订单列表数据
            }else {
              this.getOrderDetail();
            }
          },
          fail: res => {
            if (res.errMsg == 'requestPayment:fail cancel') {
              this.$util.showToast({ title: '您已取消支付' });
            } else {
              uni.showModal({ content: '支付失败,失败原因: ' + res.errMsg, showCancel: false });
            }
          }
        });
      },
    }
  }